package mz.matafe.bolao

import mz.matafe.bolao.User

/**
 * Domain Class for a Role (or "Authority").
 *
 * <p>Represents an user. Eg.: (ROLE_ADMIN,ROLE_USER,...)
 *
 * @since 0.0.1
 * @author <a href="mailto:matafe@gmail.com">Maurício T. Ferraz</a>
 */
class Role implements Comparable{

    //services
    def authenticateService

    //properties
    String code
    String description
    Date dateCreated
    Date lastUpdated
    //User createdBy
    //User updatedBy
    boolean active = true
    SortedSet users
    
    static hasMany = [users: User]

    static constraints = {
        code(unique:true, blank:false)
        description(blank:false)
        dateCreated()
        lastUpdated()
        //createdBy(nullable:true)
        //updatedBy(nullable:true)
        active()
    }

    static mapping = {
        table 'WC_ROLE'
        sort code:'asc'
        //createdBy column: 'CREATED_BY'
        //updatedBy column: 'UPDATED_BY'
    }

    /*
    def beforeInsert = {
        log.info "Before insert a Role"
        def loggedUser = authenticateService.userDomain()
        this.createdBy = this.createdBy ?: loggedUser
        this.updatedBy = updatedBy ?: loggedUser
    }

    def beforeUpdate = {
        log.info "Updating Role ${id} "
        this.updatedBy = authenticateService.userDomain()
    }

    def afterInsert = {
        log.info "Role ${id} inserted by ${createdBy}"
    }

    def afterUpdate = {
        log.info "Role ${id} updated by ${updatedBy}"
    }
*/
    String toString(){
        "${this.class.name}:id[$id];code[$code]"
    }

    int compareTo(Object o){
        this.code <=> ((Role)o).code
    }
}
